
#define UECC_DLL_API	_declspec(dllexport)

#include "LibuECC.h"
#include "../micro-ecc/uECC.h"

#define CURRENT_CURVE	uECC_secp256k1()
#define SHA256_SIZE		32

UECC_DLL_API int get_version()
{
	return 2;
}

UECC_DLL_API int LibuECC_generate_key_pair(LibuECC_private_key_t* p_private_key,
	LibuECC_public_key_t* p_public_key)
{
	int ret = uECC_make_key(p_public_key->buffer, 
		p_private_key->buffer, 
		CURRENT_CURVE);
	if (ret == 1)
	{
		return LIBUECC_SUCC;
	}
	else
	{
		return LIBUECC_ERR_UNKOWN;
	}
}

UECC_DLL_API int LibuECC_get_publickey(LibuECC_private_key_t const * const p_private_key,
	LibuECC_public_key_t* p_public_key)
{
	int ret = uECC_compute_public_key(p_private_key->buffer,
		p_public_key->buffer, 
		CURRENT_CURVE);
	if (ret == 1)
	{
		return LIBUECC_SUCC;
	}
	else
	{
		return LIBUECC_ERR_UNKOWN;
	}
}

UECC_DLL_API int LibuECC_Sign(LibuECC_private_key_t const * const p_private_key,
	LibuECC_sha256_t const * const p_sha256,
	LibuECC_signed_data_t* p_signed_data)
{
	int ret = uECC_sign(p_private_key->buffer,
		p_sha256->buffer, 
		SHA256_SIZE, 
		p_signed_data->buffer, 
		CURRENT_CURVE);
	if (ret == 1)
	{
		return LIBUECC_SUCC;
	}
	else
	{
		return LIBUECC_ERR_UNKOWN;
	}
}

UECC_DLL_API int LibuECC_get_shared_secret(LibuECC_private_key_t const * const p_private_key,
	LibuECC_public_key_t* p_public_key,
	LibuECC_shared_secret_t* p_shared_secret)
{
	int ret = uECC_shared_secret(p_public_key->buffer,
		p_private_key->buffer,
		p_shared_secret,
		CURRENT_CURVE);	
	if (ret == 1)
	{
		return LIBUECC_SUCC;
	}
	else
	{
		return LIBUECC_ERR_UNKOWN;
	}
}

UECC_DLL_API int LibuECC_is_on_Curve(LibuECC_public_key_t* p_public_key)
{
	int ret = uECC_valid_public_key(p_public_key->buffer,
		CURRENT_CURVE);
	if (ret == 1)
	{
		return LIBUECC_SUCC;
	}
	else
	{
		return LIBUECC_ERR_UNKOWN;
	}
}

UECC_DLL_API int LibuECC_verify(LibuECC_public_key_t const * const p_public_key,
	LibuECC_sha256_t const * p_sha256,
	LibuECC_signed_data_t* p_signed_data,
	bool* p_isValid)
{
	int ret = uECC_verify(p_public_key->buffer, 
		p_sha256->buffer, 
		SHA256_SIZE, 
		p_signed_data->buffer, 
		CURRENT_CURVE);
	if (ret == 1)
	{
		*p_isValid = true;
	}
	else
	{
		*p_isValid = false;
	}
	if (ret == 1)
	{
		return LIBUECC_SUCC;
	}
	else
	{
		return LIBUECC_ERR_UNKOWN;
	}
}